package substr

// https://www.php.net/manual/en/function.substr
func Do(str string, start int, length int) string{
	var r = []rune(str)
	rlen := len(r)

	if start == 0 && length == rlen {
		return str
	}

	var rstart int
	var rend int
	if start >= 0 {
		rstart = start
		rend = start + length
		if rend >= rlen{
			rend = rlen
		}
	}else{
		rend = rlen + start
		rstart = rend - length
		if rstart < 0{
			rstart = 0
		}
	}

	return string(r[rstart : rend])
}